import jsons from "../../../assets/ststic/data/Rectangle.json"

const Cesium = window['Cesium'];
let viewers;
/**
 * 初始化三维地图
 */
export default class initMap {
    viewer;
    constructor(el) {
        viewers = new Cesium.Viewer(el, {
            infoBox: false,
            selectionIndicator: false,
            navigation: false,
            animation: false,
            timeline: false,
            baseLayerPicker: false,
            geocoder: false,
            homeButton: false,
            sceneModePicker: false,
            navigationHelpButton: false,
            shouldAnimate: true,
            imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
                url: "http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer"
            }),
            // contextOptions: {
            //     //cesium状态下允许canvas转图片convertToImage
            //     webgl: {
            //         alpha: true,
            //         depth: false,
            //         stencil: true,
            //         antialias: true,
            //         premultipliedAlpha: true,
            //         preserveDrawingBuffer: true,
            //         failIfMajorPerformanceCaveat: true
            //     },
            //     allowTextureFilterAnisotropic: true
            // }
            skyAtmosphere: false
        });
        viewers.cesiumWidget.creditContainer.style.display = 'none';
        // viewers.scene.globe.enableLighting = false;
        viewers.shadows = false;
        viewers.scene.screenSpaceCameraController.enableCollisionDetection = true;

        // var terrainLayer = new Cesium.CesiumTerrainProvider({
        //     url: "https://kfq-digital-village.oss-cn-hangzhou.aliyuncs.com/terrain/lishui", // 默认立体地表
        // });
        // viewers.scene.terrainProvider = terrainLayer;



        /**
         * 放大缩小层级设置
         */
        viewers.scene.screenSpaceCameraController.minimumZoomDistance = 10;
        viewers.scene.screenSpaceCameraController.maximumZoomDistance = 150000;

        viewers.camera.setView({
            destination: Cesium.Cartesian3.fromDegrees(119.84460892444056, 27.954984315783022, 23273.692421905747),
            orientation: {
                heading: 6.27348276801108,
                pitch: -0.4546468703695412,//从上往下看为-90
                roll: 6.282284976958881
            }
        });


        this.viewer = viewers;
        this.viewer.scene.fxaa = false
        this.viewer.scene.postProcessStages.fxaa.enabled = false;
        this.viewer.imageryLayers.get(0).show = false;//不显示底图
        this.viewer.scene.globe.baseColor = new Cesium.Color.fromCssColorString('rgba(17, 19, 23, 0.0)');//设置地球颜色
        //添加geojson格式地理数据（由shp格式文件转换得来）

        Cesium.Math.setRandomNumberSeed(0);  //设置随机数种子
        var promise = Cesium.GeoJsonDataSource.load(jsons); //geojson面数据
        promise.then((dataSource) => {
            this.viewer.dataSources.add(dataSource);
            var entities = dataSource.entities.values;
            var colorHash = {};
            for (var i = 0; i < entities.length; i++) {
                var entity = entities[i];
                var name = entity.name;  //geojson里面必须得有一个name属性，entity.name对应
                var color = colorHash[name]; //可以使两个同名要素使用同一种颜色。。。

                if (!color) {
                    color = Cesium.Color.fromRandom({
                        alpha: 1.0
                    });
                    colorHash[name] = color;
                }

                // console.log(entity.polygon)
                let hei = Math.floor(Math.random() * 150 + 2);
                (hei > 0 && hei <= 5) && (entity.polygon.material = new Cesium.Color.fromCssColorString('rgba(0, 36, 41, 1.0)'));
                (hei > 5 && hei <= 10) && (entity.polygon.material = new Cesium.Color.fromCssColorString('rgba(0, 86, 97, 1.0)'));
                (hei > 10 && hei <= 25) && (entity.polygon.material = new Cesium.Color.fromCssColorString('rgba(3, 128, 145, 1.0)'));
                (hei > 25 && hei <= 50) && (entity.polygon.material = new Cesium.Color.fromCssColorString('rgba(2, 150, 170, 1.0)'));
                (hei > 50 && hei <= 75) && (entity.polygon.material = new Cesium.Color.fromCssColorString('rgba(6, 182, 206, 1.0)'));
                (hei > 75 && hei <= 100) && (entity.polygon.material = new Cesium.Color.fromCssColorString('rgba(3, 199, 224, 1.0)'));
                (hei > 100 && hei <= 152) && (entity.polygon.material = new Cesium.Color.fromCssColorString('rgba(3, 117, 224, 1)'));
                entity.polygon.outline = false;
                entity.polygon.extrudedHeight = hei;//20000~60000的随机数，单位是米
                entity.polygon.height = 50;

            }
        });


        return viewers;
    }
    //设置视角
    setView() {
        this.viewer.camera.setView({
            destination: Cesium.Cartesian3.fromDegrees(117.16, 32.71, 15000000.0)
        });
    }
    // 消除三维对象
    destroy() {
        this.viewer.entities.removeAll();
        this.viewer.imageryLayers.removeAll(true);
        this.viewer.destroy();
    }
}